require "scripts.少年光头.Helper.getMouse"
require "scripts.少年光头.Helper.deepcopy"
require "scripts.少年光头.1a.GlobalData"
UiHelper.unittip = obj:extend()
function UiHelper.unittip:new()
    --args.style = args.style or {size = 8 * 0.001}

    self.bg = UiHelper.backdrop({fdfName = "sysItemTip"}):dfsize(0, 0)
    self.lbbg = UiHelper.backdrop({parent = self.bg.frameId}):dfsize(0, 0)
    self.lb = UiHelper.label():dfsize(0, 0)
    self.lbbg:dfpoint(0, 0, FRAME_ALIGN_LEFT_TOP, self.lb.frameId, FRAME_ALIGN_LEFT_TOP):dfpoint(
        0,
        0,
        FRAME_ALIGN_RIGHT_BOTTOM,
        self.lb.frameId,
        FRAME_ALIGN_RIGHT_BOTTOM
    ):dfbackdrop(backgroundData.transparentImg)

    self.goldbgall =
        UiHelper.backdrop({parent = self.lbbg.frameId}):dfsize(0, 0.01):dfClearAllPoints():dfpoint(
        0,
        0,
        FRAME_ALIGN_LEFT_TOP,
        self.lbbg.frameId,
        FRAME_ALIGN_LEFT_BOTTOM
    ):dfpoint(0, 0, FRAME_ALIGN_RIGHT_TOP, self.lbbg.frameId, FRAME_ALIGN_RIGHT_BOTTOM):dfbackdrop(
        backgroundData.transparentImg
    )

    self.goldbg =
        UiHelper.backdrop({parent = self.goldbgall.frameId}):dfsize(0.01, 0.01):dfbackdrop(
            backgroundData.goldImg
    ):dfpointp(0, 0, self.goldbgall.frameId, FRAME_ALIGN_LEFT, FRAME_ALIGN_LEFT)

    self.goldlb =
        UiHelper.label({parent = self.goldbgall.frameId}):dfsize(0, 0.01):dfpoint(
        0,
        0,
        FRAME_ALIGN_LEFT_TOP,
        self.goldbg.frameId,
        FRAME_ALIGN_RIGHT_TOP
    ):dfpoint(0, 0, FRAME_ALIGN_LEFT_BOTTOM, self.goldbg.frameId, FRAME_ALIGN_RIGHT_BOTTOM):dftext("0")

    self.lumberbgall =
        UiHelper.backdrop({parent = self.bg.frameId}):dfClearAllPoints():dfsize(0, 0.01):dfbackdrop(
        backgroundData.transparentImg
    ):dfpoint(0, 0, FRAME_ALIGN_LEFT_TOP, self.goldbgall.frameId, FRAME_ALIGN_LEFT_BOTTOM):dfpoint(
        0,
        0,
        FRAME_ALIGN_RIGHT_TOP,
        self.goldbgall.frameId,
        FRAME_ALIGN_RIGHT_BOTTOM
    )

    self.lumberbg =
        UiHelper.backdrop({parent = self.lumberbgall.frameId}):dfpointp(
        0,
        0,
        self.lumberbgall.frameId,
        FRAME_ALIGN_LEFT,
        FRAME_ALIGN_LEFT
    ):dfsize(0.01, 0.01):dfbackdrop( backgroundData.lumberImg)

    self.lumberlb =
        UiHelper.label({parent = self.lumberbgall.frameId}):dfpoint(
        0,
        0,
        FRAME_ALIGN_LEFT_TOP,
        self.lumberbg.frameId,
        FRAME_ALIGN_RIGHT_TOP
    ):dfpoint(0, 0, FRAME_ALIGN_LEFT_BOTTOM, self.lumberbg.frameId, FRAME_ALIGN_RIGHT_BOTTOM):dftext("0")

    self.bg:dfpoint(-0.005, 0.005, FRAME_ALIGN_LEFT_TOP, self.lb.frameId, FRAME_ALIGN_LEFT_TOP):dfpoint(
        0.005,
        -0.005,
        FRAME_ALIGN_RIGHT_BOTTOM,
        self.lumberbgall.frameId,
        FRAME_ALIGN_RIGHT_BOTTOM
    )
    return self
end
---设置TIP文本
function UiHelper.unittip:setTexture(text)
    self.lb:dftext(text)
end
function UiHelper.unittip:show()
    self.bg.frame:dfshow()
    self.lb.frame:dfshow()
end
function UiHelper.unittip:hide()
    self.bg.frame:dfhide()
    self.lb.frame:dfhide()
end
---使用鼠标单位提示
function UiHelper.unittip:MouseUnintOpen()
    local c = 0
    htime.setInterval(
        0.03,
        function(curTimer)
            if hjapi.DzGetUnitUnderMouse() ~= nil then
                local id = hjapi.DzGetUnitUnderMouse()

                local name = hunit.getName(id)

                if name ~= "" and name ~= nil then
                    self:show()
                    local d = {
                        x = getMouseRX(),
                        y = getMouseRY() + 0.05,
                        point = FRAME_ALIGN_CENTER
                    }

                    local text = self:createText(id)
                    self:setTexture(text)
                    self.lb:dfAbsolutePoint(d.x, d.y, d.point)
                else
                    self:hide()
                end
            else
                self:hide()
            end
        end
    )
end

function UiHelper.unittip:createText(id)
    local txt = {}

    local name = hunit.getName(id)
    local player = hunit.getOwner(id)
    local playerName = hplayer.getName(player)
    local playerPrestige = hplayer.getPrestige(player)

    local playerGold = hplayer.getGold(player)

    local playerLumber = hplayer.getLumber(player)

    table.insert(txt, playerName)

    if playerPrestige ~= " - " then
        name = playerPrestige .. name
    end
    table.insert(txt, name)
    --table.insert(txt,"$：".. hcolor.gold( playerGold).."     "..hcolor.green( playerLumber))
    self.goldlb:dftext(hcolor.gold(playerGold))
    self.lumberlb:dftext(hcolor.gold(playerLumber))
    return table.concat(txt, "|n")
end
